package com.ntschy.common.valid.dateTimeRange;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDate;
import java.util.regex.Pattern;

public class DateTimeRangeValidationUtil {
    private static final Pattern PATTERN = Pattern.compile("^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)([ \\t\\n\\x0B\\f\\r])(([0-1]{1}[0-9]{1})|([2]{1}[0-4]{1}))([:])(([0-5]{1}[0-9]{1}|[6]{1}[0]{1}))([:])((([0-5]{1}[0-9]{1}|[6]{1}[0]{1})))/((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)([ \\t\\n\\x0B\\f\\r])(([0-1]{1}[0-9]{1})|([2]{1}[0-4]{1}))([:])(([0-5]{1}[0-9]{1}|[6]{1}[0]{1}))([:])((([0-5]{1}[0-9]{1}|[6]{1}[0]{1})))$");
    /**
     * 验证日期格式 2020-01-01 10:20:10/2020-11-11 11:22:11
     */
    public static boolean isDateRange(String dateRange){
        if (StrUtil.isEmpty(dateRange)) {
            return false;
        } else {
            boolean matches = PATTERN.matcher(dateRange).matches();
            if(matches) {
                String[] split = dateRange.split("/");
                LocalDate start = LocalDateTimeUtil.parseDate(split[0], DatePattern.NORM_DATETIME_PATTERN);
                LocalDate end = LocalDateTimeUtil.parseDate(split[1], DatePattern.NORM_DATETIME_PATTERN);
                return start.isBefore(end);
            } else {
                return false;
            }
        }
    }

}
